<?php

namespace app\api\controller\mjlbb\shopcar;
use app\api\validate\ShopCarValidate;
use app\common\controller\Api;
use think\Db;
use think\Request;

class ShopCar extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
    /**
     * 验证器
     * @var \app\api\validate\ShopCarValidate
     */
    public $validate;
    
    
    public $request;
    
    public function __construct(Request $request)
    {
        $this->validate = new ShopCarValidate();
        $this->request = $request;
    }
    
    /**
     * NAME: 购物车加购操作
     * @author Henry
     */
    public function addShopCar(Request $request)
    {
        $param = [
            'openid'            => $request->param('openid'),
            'goods_id'          => $request->param('goods_id', '' , 'intval'),
            'sku_price_id'      => $request->param('sku_price_id'), // 规格
            'goods_num'         => $request->param('goods_num'),    // 商品数量
        ];
        
        if(!$this->validate->scene('addShopCar')->check($param)){
            ajax_info(1, $this->validate->getError());
        }
        
        $shopcar = Db::table('fa_shopro_cart')
            ->where(array_slice($param, 0, 2))
            ->find();
        
        if($shopcar){
            $shopcar = Db::table('fa_shopro_cart')->where(array_slice($param, 0, 2))->update(['goods_num' => $param['goods_num']]);
        }else{
            $data = [
                'createtime' => time(),
            ];
            Db::table('fa_shopro_cart')->insert(array_merge($param, $data));
        }
        ajax_info(0, 'success', []);
    
    }
    
    /**
     * NAME: 购物车减购
     * @param  \think\Request  $request
     * @author Henry
     */
    public function shopCarDec(Request $request)
    {
        $param = [
            'openid'            => $request->param('openid'),
            'goods_id'          => $request->param('goods_id', '' , 'intval'),
            'id'                => $request->param('car_id', '' , 'intval'),    // 购物车id号
        ];
    
        if(!$this->validate->scene('shopCarDec')->message('id', 'car_id不能为空')->check($param)){
            ajax_info(1, $this->validate->getError());
        }
        
        $shopcar = Db::table('fa_shopro_cart')
            ->where($param)
            ->find();

        if($shopcar && $shopcar['goods_num'] != 1){
            Db::table('fa_shopro_cart')->where($param)->setDec('goods_num');
        }else{
            Db::table('fa_shopro_cart')->delete($param['id']);
        }
        ajax_info(0);
    
    }
    
    
    /**
     * NAME: 购物车规格修改
     * @author Henry
     */
    public function updateShopCarSefic(Request $request)
    {
        $param = [
            'openid'            => $request->param('openid'),
            'goods_id'          => $request->param('goods_id', '' , 'intval'),
            'id'                => $request->param('car_id', '' , 'intval'),            // 购物车id号
            'sku_price_id'      => $request->param('sku_price_id', '' , 'intval'),      // 购物车规格id号
        ];
        if(!$this->validate->scene('updateShopCarSefic')->message('id', 'car_id不能为空')->check($param)){
            ajax_info(1, $this->validate->getError());
        }

        $update = Db::table('fa_shopro_cart')->update(array_slice($param, 2, 2));
        ajax_info(0);
    }

    /**
     * NAME: 获取购物车列表
     * @param Request $request
     */
    public function getShopCarList(Request $request)
    {
        $param = [
            'openid'            => $request->param('openid'),
        ];
        if(!$this->validate->scene('openid')->check($param)){
            ajax_info(1, $this->validate->getError());
        }
        $shopcar = Db::table('fa_shopro_cart')
            ->field('sc.id,sc.sku_price_id,sc.sku_price_id,sc.goods_num,g.title,g.image,g.price')
            ->alias('sc')
            ->join('fa_shopro_goods g', 'sc.goods_id = g.id')
            ->where(array_slice($param, 0, 2))
            ->select();

        foreach ($shopcar as $k => $v)
        {

            $info = Db::table('fa_shopro_goods_sku_price')->field('goods_sku_ids')->where('id', $v['sku_price_id'])->find();
            $skuId = substr($info['goods_sku_ids'],2,1);
            $skuName = Db::table('fa_shopro_goods_sku')->field('name')->where('id', $skuId)->find();
            $shopcar[$k]['skuName'] = $skuName['name'];
            $shopcar[$k]['status']  = 1;
            $shopcar[$k]['image']   = config('mjlbb')['host'].$shopcar[$k]['image'];
        }
        if(!$shopcar){
            ajax_info(0, 'success', []);
        }
        ajax_info(0, 'success', $shopcar);
    }

    /**
     * NAME: 购物车加购和减购一起操作
     */
    public function shopCarOpent(Request $request)
    {
        $param = [
            'openid'            => $request->param('openid'),
            'id'                => $request->param('id'),           // 购物车id号
            'goods_num'         => $request->param('goods_num'),    // 商品数量
        ];

        if(!$this->validate->scene('shopCarOpent')->check($param)){
            ajax_info(1, $this->validate->getError());
        }

        if($param['goods_num'] > 0){
            Db::table('fa_shopro_cart')->where(['id'=>$param['id']])->update(['goods_num' => $param['goods_num']]);
        }else if($param['goods_num'] == 0){
            Db::table('fa_shopro_cart')->delete($param['id']);
        }
        ajax_info(0, 'success', []);
    }
}
